﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DungeonCraft.DataModel.Abilities;
using DungeonCraft.DataModel.Interfaces;
using DungeonCraft.DataModel.Abilities.Guards;
using DungeonCraft.DataModel.Tools;

namespace DungeonCraft.DataModel.AI
{
    public class ZombieAI
    {
        private List<Player> zombies;
        private Random rnd;
        private WalkUp walkUp;
        private ILevel level;
        private WalkingGuard walkGuard;
        private WalkDown walkDown;
        private WalkLeft walkLeft;
        private WalkRight walkRight;
        private HitDetector hitDetector;
        private ManaGuard managuard;

        public ZombieAI(ILevel level, ICallbackTimer timer, HitDetector hitDetector)
        {
            zombies = new List<Player>();
            rnd = new Random();
            walkUp = new WalkUp();
            walkDown = new WalkDown();
            walkLeft = new WalkLeft();
            walkRight = new WalkRight();
            this.hitDetector = hitDetector;
            
            this.level = level;
            walkGuard = new WalkingGuard();
            managuard = new ManaGuard();
            timer.TickCallBack = Execute;
            timer.tickTime = 50;
            timer.Start();
        }


        public void addZombie(Player zombie)
        {
            zombies.Add(zombie);
        }

        public void Execute()
        {
            foreach (Player zombie in zombies)
            {
                if (!zombie.Dead)
                {
                    double rd = rnd.NextDouble();
                    switch (Convert.ToInt32(rd * 4))
                    {
                        case 0:
                            walkUp.Execute(level, zombie, walkGuard, null);
                            break;
                        case 1:
                            walkDown.Execute(level, zombie, walkGuard, null);
                            break;
                        case 2:
                            walkRight.Execute(level, zombie, walkGuard, null);
                            break;
                        case 3:
                            walkLeft.Execute(level, zombie, walkGuard, null);
                            break;
                    }

                    if (Convert.ToInt32(rnd.NextDouble() * 50) == 19)
                    {
                        MagicMissile magicMissile = new MagicMissile(new CallbackTimer(), hitDetector);
                        magicMissile.Execute(level, zombie, managuard, new Location( zombie.Location.X + Convert.ToInt32((rnd.NextDouble() - 0.5) * 100), zombie.Location.Y + Convert.ToInt32((rnd.NextDouble() - 0.5) * 100)));

                    }
                }
            }
        }


    }
}
